package com.ssfGame.puremvc.view.module.queueload
{
	
	import com.game.utils.GlobalFun;
	import com.game.utils.MyTimer;
	import com.ssfGame.puremvc.StaticConst;
	import com.ssfGame.puremvc.model.ConfigProxy;
	import com.ssfGame.puremvc.model.vo.ConfigVO;
	import com.ssfGame.puremvc.model.vo.LoadVO;
	import com.ssfGame.puremvc.view.module.queueload.vc.QueueLoadBigVC;
	import com.ssfGame.puremvc.view.module.queueload.vc.QueueLoadSmallVC;
	import com.ssfGame.puremvc.view.module.queueload.vc.QueueLoadVC;
	import com.ssfGame.puremvc.view.module.queueload.vo.QueueLoadVO;
	import com.ssfGame.puremvc.view.module.queueload.vo.ShowLoadVO;
	
	import flash.display.Sprite;
	
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	/**
	 * 列队加载面板
	 * @author chery
	 */
	public class QueueLoadPanelMediator extends Mediator
	{
		/**
		 * 
		 * @default 
		 */
		
		public static const NAME:String="queueLoadPanelMediator";
		private var _configVO:ConfigVO;
		private var _queueLoadVC:QueueLoadVC;
		private var _isFirst:Boolean=true;
		private var _smallLoadTimer:MyTimer
		private var _loadOutTime:uint;
		private var _loadList:Array=[];
		
		/**
		 * 列队加载
		 * @param mediatorName
		 * @param viewComponent
		 */
		public function QueueLoadPanelMediator(mediatorName:String=null, viewComponent:Object=null)
		{
			super(mediatorName, viewComponent);
			_configVO=(facade.retrieveProxy(ConfigProxy.NAME) as ConfigProxy).configVO;
		}
		override public function listNotificationInterests():Array{
			return [
				StaticConst.N_M_QUEUE_LOAD_PROGRESS,
				StaticConst.N_M_QUEUE_LOAD_BIG_SHOW,
				StaticConst.N_M_QUEUE_LOAD_SMALL_SHOW,
				StaticConst.N_M_QUEUE_LOAD_PANEL_DEL
			]
			
		}
		override public function onRegister():void{
			_smallLoadTimer=new MyTimer();
			_loadOutTime=_configVO.loadTimeOut;
		}
		override public function handleNotification(notification:INotification):void{
			var queueLoadVO:QueueLoadVO=notification.getBody() as QueueLoadVO;
			var _showLoadVO:ShowLoadVO;
			switch (notification.getName()){
				case StaticConst.N_M_QUEUE_LOAD_PROGRESS:
					if(_queueLoadVC==null){
						return;
					}
					var _currentIndex:Number=Number(notification.getType());
					var loadVO:LoadVO=queueLoadVO.loadVec[_currentIndex];
					var _per:uint=loadVO.progress;
					_queueLoadVC.disTxt.text=_per+"%	"+(_currentIndex+1)+"/"+queueLoadVO.loadVec.length;
					setSmallTimeOut();
					break
				case StaticConst.N_M_QUEUE_LOAD_BIG_SHOW:
					
					_showLoadVO=notification.getBody() as ShowLoadVO;
					
					showLoadVC(_showLoadVO,new QueueLoadBigVC());
					GlobalFun.setDisCenter(_queueLoadVC,StaticConst.appStage);
					break
				case StaticConst.N_M_QUEUE_LOAD_SMALL_SHOW:
					
					_showLoadVO=notification.getBody() as ShowLoadVO;
					
					showLoadVC(_showLoadVO,new QueueLoadSmallVC());
					
					GlobalFun.setDisCenter(_queueLoadVC,StaticConst.appStage);
					showMask(true);
					
					break
				case StaticConst.N_M_QUEUE_LOAD_PANEL_DEL:
					var _delId:String=notification.getBody() as String;
					if(_delId==null){
						_delId="0";
					}
					delLoadVC(_delId);
					break
			}
		}
		private function delLoadVC(delId:String="0"):void{
			for(var i:uint=0;i<_loadList.length;i++){
				if(delId==_loadList[i]){
					_loadList.splice(i,1);
				}
			}
			if(_loadList.length>0){
				return;
			}
			_smallLoadTimer.gc();
			showMask(false);
			sendNotification(StaticConst.N_M_LOAD_LAYER_DEL,_queueLoadVC);
			_queueLoadVC=null;
		}
		private function showLoadVC(showLoadVO:ShowLoadVO,vc:QueueLoadVC):void{
			
			if(showLoadVO!=null&&!checkHaveLoadId(showLoadVO.loadId)){
				_loadList.push(showLoadVO.loadId);
			}
			if(_queueLoadVC==null){
				_queueLoadVC=vc;
				sendNotification(StaticConst.N_M_LOAD_LAYER_ADD,_queueLoadVC);
			}
			if(_queueLoadVC!=null){
				_queueLoadVC.disTxt.text="0%";
			}
		}
		private function checkHaveLoadId(loadId:String):Boolean{
			for(var i:uint=0;i<_loadList.length;i++){
				if(loadId==_loadList[i]){
					return true;
				}
			}
			return false;
		}
		
		private function showMask(bool:Boolean = false):void
		{
			if(!_queueLoadVC || !_queueLoadVC.stage)
				return;
			var p:Sprite = _queueLoadVC.parent as Sprite;
			p.graphics.clear();
			if(bool)
			{
				p.graphics.beginFill(0, 0);
				p.graphics.drawRect(0, 0, _queueLoadVC.parent.stage.stageWidth, _queueLoadVC.parent.stage.stageHeight);
				p.graphics.endFill();
			}
		}
		private function setSmallTimeOut():void{
			_smallLoadTimer.gc();
			_smallLoadTimer.setTimer(function ():void{
				sendNotification(StaticConst.N_M_QUEUE_LOAD_PANEL_DEL);
			},_loadOutTime*1000,1);
		}
		
	}
	
}